//
//  MainViewController.swift
//  我的微博
//
//  Created by 李献红 on 15/10/8.
//  Copyright © 2015年 cn.lixianhong. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


        addChildViewControllers()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
///  添加所有的子控制器
    //测试能否提交
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //此时 tabBarButton 都已创建
        setupComposedButton()
        
    }
    
    
    /// 运行循环监听到事件后，向 VC 发送消息，动态执行 方法，因此不能设置为 private
    func clickComposedButton() {
        
        //判断是否登录，如果登录发微博，没有登录就跳转到登录授权页面
        let vc = UserAccount.userLogin ? ComposeViewController() : OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }

    
 /// 设置按钮的位置
    private func setupComposedButton(){
    
        let w = tabBar.bounds.width / CGFloat((viewControllers?.count)!)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
        composeButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
     
    }
    
    
    /// 按钮懒加载
    lazy private var composeButton: UIButton = {
    
        //自定义类型的按钮
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        
        return button
    }()
    
    
    
///  抽取出添加子控制器的方法
    private func addChildViewController(vc:UIViewController,title:String,imageName: String) {
        
        //在Appdelegate中进行了设置
//        tabBar.tintColor = UIColor.orangeColor()
        
        //title
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    


}
